package cn.itcast.bos.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.StandardService;

@ParentPackage(value="json-default")
@Namespace("/")
@Actions// 表示这个类是一个struts2的类，可以省略
@Controller
@Scope(value="prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{
	
	Standard standard = new Standard();
	
	@Override
	public Standard getModel() {
		return standard;
	}
	
	//注入Service
	@Autowired// 默认是按照类型匹配
	private StandardService standardService;

	/**保存*/
	@Action(value="standard_save",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String save(){
		standardService.save(standard);
		return SUCCESS;
	}

	/**验证收派标准名称不能出现重复*/
	@Action(value="standard_validate",results={@Result(name="success",type="json")})
	public String validateName(){
		// 获取收派标准名称
		String name = standard.getName();
		// 使用收派标准名称查询Standard表，hql语句from Standard where name = ?
		List<Standard> list = standardService.findByName(name);
		// 没有查询到结果，说明当前收派标准名称可以使用
		if(list!=null && list.size()==0){
			ServletActionContext.getContext().getValueStack().push(true);
		}
		else{
			ServletActionContext.getContext().getValueStack().push(false);
		}
		return SUCCESS;
	}
	
	// 属性驱动，接收分页参数
	private Integer page;
	private Integer rows;
	
	public void setPage(Integer page) {
		this.page = page;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	/**首页查询（分页）*/
	@Action(value="standard_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		//查询数据
		Pageable pageable = new PageRequest(page-1, rows);// 当前页(0作为基页数)、当前页最多显示的记录数
		Page<Standard> page = standardService.findPageQuery(pageable);
		// struts2传递json数组
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("total", page.getTotalElements());// 总记录数
		map.put("rows", page.getContent());// 返回的数据
		ServletActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
	
	// 传递字符串ID（中间使用逗号分开）
	private String ids;
	
	public void setIds(String ids) {
		this.ids = ids;
	}

	// 批量删除
	@Action(value="standard_delete",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String delete(){
		standardService.deleteByIds(ids);
		return SUCCESS;
	}
	
	// 查询所有收派标准
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
	public String findAll(){
		List<Standard> list = standardService.findAll();
		ServletActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
}
